package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.EquipmentAccountRegister;
import com.ruoyi.system.mapper.EquipmentAccountRegisterMapper;
import com.ruoyi.system.service.EquipmentAccountRegisterService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 设备+账户登记表(EquipmentAccountRegister)表服务实现类
 *
 * @author makejava
 * @since 2022-12-09 14:04:34
 */
@Service("equipmentAccountRegisterService")
public class EquipmentAccountRegisterServiceImpl implements EquipmentAccountRegisterService {
    @Resource
    private EquipmentAccountRegisterMapper equipmentAccountRegisterDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public EquipmentAccountRegister queryById(Integer id) {
        return this.equipmentAccountRegisterDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param equipmentAccountRegister 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<EquipmentAccountRegister> queryByPage(EquipmentAccountRegister equipmentAccountRegister, PageRequest pageRequest) {
        long total = this.equipmentAccountRegisterDao.count(equipmentAccountRegister);
        return new PageImpl<>(this.equipmentAccountRegisterDao.queryAllByLimit(equipmentAccountRegister, pageRequest), pageRequest, total);
    }

    @Override
    public List<EquipmentAccountRegister> queryAll(EquipmentAccountRegister equipmentAccountRegister) {
        return equipmentAccountRegisterDao.queryAll(equipmentAccountRegister);
    }

    /**
     * 新增数据
     *
     * @param equipmentAccountRegister 实例对象
     * @return 实例对象
     */
    @Override
    public EquipmentAccountRegister insert(EquipmentAccountRegister equipmentAccountRegister) {
        //完成数据唯一性校验;
        EquipmentAccountRegister check = new EquipmentAccountRegister();
        check.setEquipmentId(equipmentAccountRegister.getEquipmentId());
        check.setAccountId(equipmentAccountRegister.getAccountId());

        long count = equipmentAccountRegisterDao.count(check);
        if (count > 0) {
            return equipmentAccountRegister;
        }

        this.equipmentAccountRegisterDao.insert(equipmentAccountRegister);
        return equipmentAccountRegister;
    }

    /**
     * 修改数据
     *
     * @param equipmentAccountRegister 实例对象
     * @return 实例对象
     */
    @Override
    public EquipmentAccountRegister update(EquipmentAccountRegister equipmentAccountRegister) {
        this.equipmentAccountRegisterDao.update(equipmentAccountRegister);
        return this.queryById(equipmentAccountRegister.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.equipmentAccountRegisterDao.deleteById(id) > 0;
    }
}
